package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Employee;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.websocket.server.PathParam;
import java.util.Collections;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
     PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        //下一条sql进行分页，自动加入limit关键字分页
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        List<DishVO> records = page.getResult();
        long total = page.getTotal();

        return new PageResult(total,records);
    }

    @Override
    @Transactional
    public void saveWithFlavor(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //向菜品插入1条数据
        dishMapper.insert(dish);

//        获取insert语句生成的主键值
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            flavors.forEach(item->{
                item.setDishId(dishId);
            });
            //向口味表插入多条数据
            dishFlavorMapper.insertBatch(flavors);

        }


    }

//    /**
//     * 修改菜品
//     * @param dishDTO
//     */
//
//    @Override
//    public void update(DishDTO dishDTO) {
//        Dish dish = new Dish();
//        BeanUtils.copyProperties(dishDTO,dish);
//        dishMapper.update(dish);
//        Long dishId = dish.getId();
//        List<DishFlavor> flavors = dishDTO.getFlavors();
//        if(flavors != null && flavors.size() > 0) {
//            flavors.forEach(item -> {
//                item.setDishId(dishId);
//            });
//            //向口味表插入多条数据
//            dishFlavorMapper.updateBatch(flavors);
//        }
//
//    }

    @Override
    public  DishVO getByIdWithFlavor(Long id) {
        Dish dish =  dishMapper.getById(id);
        List<DishFlavor> dishFlavorsList = dishFlavorMapper.getByDishId(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavorsList);
        return dishVO;
    }

    @Override

    public void delete(List<Long> ids) {
//        判断是否起售
        ids.forEach(item->{
            Dish dish = dishMapper.getById(item);
            if(dish.getStatus() == StatusConstant.ENABLE){
                throw new RuntimeException(MessageConstant.DISH_ON_SALE);
            }
        });

//        判断是否关联套餐
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishIds(ids);
        if(setmealIds != null && setmealIds.size()>0){
            throw new RuntimeException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
//      遍历删除
        ids.forEach(item -> {
            dishMapper.deleteById(item);
            dishFlavorMapper.deleteById(item);

        });
    }

    /**
     * 修改
     * @param dishDTO
     */
    @Override
    public void updateWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
//        修改菜品基本信息
        dishMapper.update(dish);

//        删除原有的口味
        dishFlavorMapper.deleteById(dishDTO.getId());
//        重新插入新的口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0) {
            flavors.forEach(item -> {
                item.setDishId(dishDTO.getId());
            });
            //向口味表插入多条数据
            dishFlavorMapper.insertBatch(flavors);
        }



    }

    /**
     * 起售停售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {;
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        dishMapper.update(dish);

    }

    @Override
    public List<DishVO> list(Long categoryId) {
        // 2. 处理可能的null结果
        List<DishVO> dishList = dishMapper.list(categoryId);
        return dishList != null ? dishList : Collections.emptyList();
    }


}
